<template>
	<view class="content" :style="{backgroundImage:`url(${indexBackgroundImage})`}">
		<view class="main">
			<view class="title">
				<view class="h1">
					<text>企业登录</text>
				</view>
<!--				<view class="h2">-->
<!--					<text>快加入我们吧~</text>-->
<!--				</view>-->
			</view>
			<view class="login">
				<view class="tab">
					<view class="tab-1">
<!--						<text class="active">登录</text>-->
					</view>
<!--					<view class="tab-1">-->
<!--						<text>注册</text>-->
<!--					</view>-->
				</view>
				<view class="tel">
					<image src="/static/login/tel.png" mode=""></image>
					<u-input type="number"  placeholder="请输入手机号"
					placeholder-style="{
						width: 200px;
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #fff;
					}"
                 v-model="phone"
                 maxlength="11"/>
				</view>
        <view class="tel" v-if="isBoolPhone&&isBoolPhone.status" @click="selectShow=true">
          {{selectValue}}
        </view>
				<view class="pwd">
					<image src="/static/login/pwd.png" mode=""></image>
					<u-input type="text"  placeholder="请输入验证码"
					placeholder-style="{
						width: 200px;
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #fff;
					}"
                 v-model="code"
                 maxlength="6" />
					<button type="default" :disabled="opt.isSend" @click="onBtnCode">{{opt.codeText}}</button>
				</view>
				<view class="other" >
<text @tap="nextPage(`/pages/login/login?type=big`)">密码登录</text>
				<!-- <text @tap="nextPage(`/pages/loginForget/loginForget?type=big`)">忘记密码</text>-->
			</view>
				<view class="txt">
					<u-radio-group>
						<u-radio active-color="#2CBF7E"></u-radio>
					</u-radio-group>
					<text>我已阅读并同意<text class="deep" @click="onUserAgreement">《用户协议》</text>和<text class="deep" @click="onPrivacypolicy">《隐私政策》</text></text>
				</view>
				<view class="btn">
					<button @click="onLogin" type="default">登录</button>
				</view>
			</view>
		</view>
    <u-select v-model="selectShow"  :list="selectList" @confirm="confirmSelect"
              :default-value="selectIndex"
    ></u-select>
	</view>
</template>

<script>
	import indexBackgroundImage from "@/static/login/bg1.png"
  import sendCodeMixins from '@/mixins/sendCode-mixins'
  import {isMPStrict} from '@/utils/utils'
  import {getCompanyListByMobile,companyUserMobileLogin} from '@/api/login'
  import { mapActions } from 'vuex'
	export default {
    mixins:[sendCodeMixins],
		data() {
			return {
				indexBackgroundImage: indexBackgroundImage,
        selectShow:false,
        selectId:'',//对应公司id
        selectValue:'请选择公司',//对应公司值
        selectList:[],//数据
        selectIndex:[],//在那一项
			};
		},
    computed: {
      //手机号码是否为空或者不合法
      isBoolPhone(){
        if(this.phone.length&&isMPStrict(this.phone)){
          return {
            status:this.phone.length&&isMPStrict(this.phone),
            userPhone:this.phone
          }
        }
      }
    },
    watch: {
      'isBoolPhone.userPhone'(userPhone,b){
        if(userPhone){
         this.getCompanyListByMobile(userPhone)
        }
      }
    },
		methods: {
      ...mapActions(['setUserInfo','setUserToken']),
      //用户协议
      onUserAgreement(){
        this.$navigateTo('/pages/userAgreement/userAgreement')
      },
      //隐私政策
      onPrivacypolicy(){
        this.$navigateTo('/pages/privacypolicy/privacypolicy')
      },
      //企业用户手机验证码登录
      companyUserMobileLogin(data){
        companyUserMobileLogin(data).then(res=>{
          this.$hideLoading()
          const {code, message, success,result:{
            token,userInfo:user
          }} = res
          if(code===200&&success){
            this.$showToast(message)
            this.setUserInfo({user})
            this.setUserToken({token})
            const urlPath = uni.getStorageSync('url-path')
            if (!urlPath || urlPath == '/pages/login/login') {
              this.$u.route({
                animationDuration:500,
                url: '/pages/index/index',
                type:'reLaunch'
              })
            }else {
              uni.reLaunch({
                url: urlPath,
                success: () => {
                  //成功后删除
                  uni.removeStorageSync('url-path')
                },
              })

            }

          } else {
            this.$showToast(message)
          }
        }).catch(err=>{
          console.log ( err )
        })
      },
      //根据手机号码获取公司列表
      getCompanyListByMobile(userPhone){
        getCompanyListByMobile(userPhone).then(res=>{
          const {code,result} = res
          let list = []
          if(code===200){
            result.map(k=>{
              list.push( {
                label:k.companyName,
                value:k.id
              })
            })

            this.selectList = list
            const value = this.selectList[0]
            this.selectId=value.value
            this.selectValue=value.label
            this.selectIndex=[0]
          }

        })
        .catch(e=>{
          console.log ( e )
        })
      },
			//跳转列表对应页面
			nextPage(url) {
				uni.navigateTo({
					url: url,
					fail() {
						uni.switchTab({
							url: url,
							fail() {
								uni.showToast({
									title: '暂未开通，敬请期待！',
									icon: 'none'
								});
							}
						});
					}
				});
			},
      //选择公司
      confirmSelect(data){
        const {value,label} = data[0]
        if(this.selectId===value){
          return
        }
        this.selectValue =label
        this.selectId =value
        const index= this.selectList.findIndex(item=>{
          return item.value===value
        })

        this.selectIndex=[index]
      },
      //登录
      onLogin(){
        if(this.onPhoneCode()){
          if(!this.code.length){
            this.$showToast('请输入验证码')
            return
          }
          const data ={
            "captcha": this.code,
            "companyId": this.selectId,
            "userPhone": this.phone
          }
          this.$showLoading({title:'登录中...'})
         this.companyUserMobileLogin(data)
        }
      }
		}
	}
</script>

<style lang="scss">
.content{
	height: 100vh;
	background-repeat: no-repeat;
	background-size: 100% 100%;
	.main{
		padding: 0 30rpx;
		.title{
			padding-top: 120rpx;
			.h1{
				margin-bottom: 20rpx;
				text{
					font-size: 60rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
				}
			}
			.h2{
				margin-bottom: 60rpx;
				text{
					font-size: 38rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
				}
			}
		}
		.login{
			padding: 30rpx 30rpx 0 30rpx;
			height: 700rpx;
			border-radius: 50rpx;
			border: #B8C0CB 2rpx solid;
			box-shadow: 0 4rpx 12rpx 0 RGBA(182, 191, 199, 1);
			.tab{
				display: flex;
				justify-content: space-around;
				margin: 30rpx 0;
				.active{
					padding: 10rpx 40rpx;
					font-size: 34rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #62C2B5;
					background: #C9EBE4;
					border-radius: 40rpx;
				}
				text{
					padding: 10rpx 40rpx;
					font-size: 34rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					border-radius: 40rpx;
				}
			}
			.tel{
				display: flex;
				align-items: center;
				margin: 60rpx 0 30rpx 0;
				padding: 20rpx;
				height: 100rpx;
				border-radius: 50rpx;
				border: #B8C0CB 2rpx solid;
				box-shadow: 0 4rpx 12rpx 0 RGBA(182, 191, 199, 1);
				image{
					width: 50rpx;
					height: 50rpx;
				}
				input{
					margin-left: 14rpx;
				}
			}
			.pwd{
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;
				padding: 20rpx;
				height: 100rpx;
				border-radius: 50rpx;
				border: #B8C0CB 2rpx solid;
				box-shadow: 0 4rpx 12rpx 0 RGBA(182, 191, 199, 1);
				image{
					width: 50rpx;
					height: 50rpx;
				}
				input{
					margin-left: 14rpx;
				}
				button{
					padding: 0;
					width: 180rpx;
					height: 60rpx;
					font-size: 24rpx;
					border-radius: 50rpx;
					border: #B8C0CB 2rpx solid;
					box-shadow: 0 4rpx 12rpx 0 RGBA(182, 191, 199, 1);
					background: none;
					color: #FFFFFF;
				}
			}
			.other{
				display: flex;
				justify-content: space-between;
				margin-bottom: 50rpx;
				padding: 0 20rpx;
				text{
					font-size: 21rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FEFFFF;
				}
			}
			.txt{
				margin-bottom: 30rpx;
				text-align: center;
				text{
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
				}
				.deep{
					color: #83E9D7;
				}
			}
			.btn{
				button{
					background: #58BEAF;
					border-radius: 50rpx;
					color: #FFFFFF;
					font-weight: bold;
					font-size: 34rpx;
					letter-spacing: 5rpx;
				}
			}
		}
	}
}
/deep/ .u-input__input{
  color: #fff;
}
</style>
